﻿using System;
using UnityEngine;
using BridgeShips.GUISystem;
using System.Collections.Generic;
using UnityEngine.UI;

namespace BridgeShips.Control {
    public class BuildingLineChartUI : GUIBehaviour {

        [SerializeField]
        private Window m_Window;

        [SerializeField]
        private Camera m_GUICamera;

        [Header( "Audio" )]
        [SerializeField]
        private SoundPlayer m_RefreshAudio;

        [SerializeField]
        private SoundPlayer m_SelectButtonAudio;

        private BuildingCategory[] m_Categories;

        private UILineChartGraph m_UILineChartGraph;

        private Button btn;

        private void Update() {
            if (!m_Window.IsOpen)
                return;

            float scrollValue = Player.ScrollValue.Get();
          
        }

        private void Start() {
            Player.LineCharts.AddStartTryer( TryStart_LineChartUI );
            Player.LineCharts.AddStopTryer( TryStop_LineChartUI );

            InventoryController.Instance.State.AddChangeListener( OnChanged_InventoryState );

            m_UILineChartGraph = transform.GetComponent<UILineChartGraph>();

            //btn = transform.GetChild( 3 ).GetComponent<Button>();
            btn = transform.Find( "ExitButton" ).GetComponent<Button>();
            btn.onClick.AddListener( BtnClick );
        }

        public bool isVisable() {
            return Player.LineCharts.Active;
        }

        public void FillDataBuf( uint id, LinkedList<Vector3> list ) {
            //bool beContain = lineChartDict.ContainsKey( id );
            //if (beContain) {
            //    lineChartDict[id].UpdateBuf( list );
            //}
            //else {
            //    UILineChartGraph newLineC = new UILineChartGraph();
            //    newLineC.Awake();
            //    newLineC.UpdateBuf( list );
            //    lineChartDict.Add( id, newLineC );
            //}
            if (m_UILineChartGraph != null) {
                m_UILineChartGraph.UpdateBuf( list );
                m_UILineChartGraph.LineChart.ShowUnit();
            }
        }

        private void OnChanged_InventoryState() {
            var inventoryClosed = InventoryController.Instance.IsClosed;
            if (!inventoryClosed) {
                while (Player.LineCharts.Active)
                    Player.LineCharts.TryStop();
            }
        }

        //ship list 

        private bool TryStart_LineChartUI() {
            if (!InventoryController.Instance.IsClosed)
                return false;
            m_Window.Open();
            //获取船的数据
            var listObjLine = ShipsDictionary.Instance.shipDicKey;
            Debug.Log( listObjLine.Count );
            transform.GetComponent<BrokentLineList>().ShowAllToggle( listObjLine );
            return true;
        }

        private bool TryStop_LineChartUI() {
            m_Window.Close();
            return true;
        }

        void BtnClick() {
            PlayerInputHandler.curActivity = null;
            GameController.LocalPlayer.LineCharts.TryStop();
        }

        
    }
}
